<?php
/*
 * Gepard db installation script, (c) Taras Kudla, 2010
 */
require_once("../core/init.php");
/*
 * Checking if i'm running in console
 */
if( !$Gepard->isconsole()){
    print "This application can't be run through web interface";
    exit;
}
$c = $Gepard->getObject("console");
$c->writeln("Welcome to Gepard basic system installation to the database");
$c->writeln ("Please, select your database engine:\n");
function error($message=null){
   print "\n\n".(($message==null)?"Some error happened":$message).", try again\nBye\n";
   die();
}
$dbos = $Gepard->dbo->getsupported();
for($i=1;$i<=count($dbos);$i++){
	print "\t$i. ".$dbos[$i-1]."\n";
}
$s = $Gepard->console->read("Enter a number: ");
if( is_numeric($s) && (int)$s>=1 && (int)$s<=count($dbos)){
$dbsel = $dbos[(int)$s-1];
} else {
  error();
}
$dbserv = $Gepard->console->read("Please, enter your database server: ");	
$dbdb = $Gepard->console->read("Please, enter your database name on server: ");	
$dbuser = $Gepard->console->read("Please, enter your database user: ");
$dbpasswd = $Gepard->console->read("Please, enter your database password: ");	

print "Trying to connect to the database...";
$conf= array("server"=>$dbserv,"db"=>$dbdb,"user"=>$dbuser,"password"=>base64_encode($dbpasswd));
$con = $Gepard->dbo->probeconnection($dbsel,$conf);
if( !$con ){
  error("Cannot connect to database");  
}
$Gepard->dbo->load($dbsel,$conf);
# requiring install.object
require_once("install.object.php");
$install = new install($Gepard);
$install->sqlinstall();
print "\nInstallation was successfull\n\n";